//
//  WaitingView.m
//  Chamleon-template
//
//  Created by YLW on 15/10/21.
//
//

#import "WaitingView.h"
#import "UIColor+Extension.h"


@implementation NativeUIWaitInfo
- (instancetype)init
{
    if (self = [super init])
    {
        self.width = 100;
        self.height = 100;
        self.color = [UIColor whiteColor];
        self.size = 12;
        self.textalign = WaitingViewTextalignCenter;
        self.padding = 0;
        self.background = [UIColor blackColor];
        self.style = WaitingViewStyleWhite;
        self.modal = NO;
        self.round = 10;
        self.padlock = YES;
        self.title = @"请等待...";
        self.times = 5;
    }
    return self;
}
//+ (instancetype)nativeUIWaitInfo:(NSDictionary *)dict
//{
//    return  [[self alloc] initWithDict:dict];
//}
//- (instancetype)initWithDict:(NSDictionary *)dict
//{
//    if (self = [super init])
//    {
//        [self setValuesForKeysWithDictionary:dict];
//    }
//    return self;
//}
- (void)setWidth:(CGFloat)width
{
    if (width >= 100)
    {
        _width = width;
    }
    else
    {
        _width = 100;
    }

}

- (void)setHeight:(CGFloat)height
{
    if (height >= 100)
    {
        _height = height;
    }
    else
    {
        _height = 100;
    }
}

- (void)setNativeUIWaitInfo:(NSDictionary *)dict
{
    if (dict)
    {
        [self setValuesForKeysWithDictionary:dict];
    }
}

- (void)setValue:(id)value forKey:(NSString *)key
{
    if ([key isEqualToString:@"color"])
    {
        self.color = [UIColor hexStringToColor:value];
    }
    else if ([key isEqualToString:@"background"])
    {
        self.background = [UIColor hexStringToColor:value];
    }
    else if ([key isEqualToString:@"textalign"])
    {
        if ([value isEqualToString:@"left"])
        {
            self.textalign = WaitingViewTextalignLet;
        }
        else if ([value isEqualToString:@"right"])
        {
            self.textalign = WaitingViewTextalignRight;
        }
        else
        {
            self.textalign = WaitingViewTextalignCenter;
        }
    }
    else if([key isEqualToString:@"style"])
    {
        if ([value isEqualToString:@"black"])
        {
            self.style = WaitingViewStyleBlack;
        }
        else
        {
            self.style = WaitingViewStyleWhite;

        }
    }
    else
    {
        [super setValue:value forKey:key];
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{}
@end

@implementation WaitingView

- (instancetype)init
{
    if (self = [super init])
    {
        [self setUI];
    }
    return self;
}

+ (instancetype)sharedWaitingView
{
    static WaitingView *waitingView;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        waitingView = [[WaitingView alloc] init];
    });
    return waitingView;
}

- (void)setUI
{
    NativeUIWaitInfo *waitInfo = [[NativeUIWaitInfo alloc] init];

    self.viewInfo = waitInfo;
    [self addSubview:self.titleLabel];
    [self addSubview:self.timeLabel];
    
    if(![self isDescendantOfView:self.activityIndicatiorView])
    {
        [self setUIAttribute];
    }
    
    [self addSubview:self.activityIndicatiorView];

    [self.activityIndicatiorView startAnimating];
}

- (void)setUIAttribute
{
    self.backgroundColor = self.viewInfo.background;
    self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - self.viewInfo.width) *0.5,([UIScreen mainScreen].bounds.size.height - self.viewInfo.height)*0.5, self.viewInfo.width, self.viewInfo.height);
    self.layer.cornerRadius = self.viewInfo.round;
    self.layer.masksToBounds = YES;
    self.titleLabel.textColor = self.viewInfo.color;
    self.titleLabel.font = [UIFont systemFontOfSize:self.viewInfo.size];
    NSTextAlignment alignment;
    switch (self.viewInfo.textalign)
    {
        case WaitingViewTextalignLet:
            alignment = NSTextAlignmentLeft;
            break;
        case WaitingViewTextalignCenter:
            alignment = NSTextAlignmentCenter;
            break;
        case WaitingViewTextalignRight:
            alignment = NSTextAlignmentRight;
            break;
        default:
            alignment = NSTextAlignmentCenter;
            break;
    }
    self.titleLabel.textAlignment = alignment;
    self.titleLabel.text = self.viewInfo.title;
    
    self.timeLabel.textColor = self.viewInfo.color;
    self.timeLabel.font = [UIFont systemFontOfSize:_viewInfo.size];
    self.timeLabel.text = [NSString stringWithFormat:@"%zd",self.viewInfo.times];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    
    self.activityIndicatiorView.center = CGPointMake(self.viewInfo.width * 0.5, CGRectGetMinY(self.titleLabel.frame) - 15);
    self.activityIndicatiorView.activityIndicatorViewStyle = self.viewInfo.style == WaitingViewStyleBlack ? UIActivityIndicatorViewStyleGray : UIActivityIndicatorViewStyleWhite;
    [self startTimer];
}

- (void)setViewInfo:(NativeUIWaitInfo *)viewInfo
{
    _viewInfo = viewInfo;
    
    
    [self setUIAttribute];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (self.viewInfo.padlock)
    {
        self.disMissVc();
        [self.timer invalidate];
    }
}

#pragma mark - 定时器
- (void)startTimer
{
    if (self.timer)
    {
        return;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(executeTimerMethod) userInfo:nil repeats:YES];
}

- (void)executeTimerMethod
{
    self.timeLabel.text = [NSString stringWithFormat:@"%zd",--self.viewInfo.times];
    
    if (self.viewInfo.times == 0)
    {
        [self.timer invalidate];
        self.disMissVc();
    }
    
}
#pragma mark - 懒加载
- (UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.frame = CGRectMake(0, self.viewInfo.height *0.5 - 10, self.viewInfo.width, 20);
    }
    return _titleLabel;
}

- (UIActivityIndicatorView *)activityIndicatiorView
{
    if (_activityIndicatiorView == nil)
    {
        _activityIndicatiorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    }
    return _activityIndicatiorView;
}
- (UILabel *)timeLabel
{
    if (_timeLabel == nil)
    {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), self.viewInfo.width, 20);

    }
    return _timeLabel;
}





@end
